/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： user.js
 * Date：18-11-6 下午3:16
 * Author: guomw
 */


$(function () {
    jsHelper.init();
});
var jsHelper = {
    localData: null,
    templateList: $("#template_list").html(),
    menusbtnTemplate: $("#menus_btn_template").html(),
    params: {
        keyword: "",
        status: -1,
        pageIndex: 1,
        barCode: ""
    },
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/user/ajax/genetesting", self.params, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 1) {
                var data = ret.data;
                self.localData = data.list;
                var appendHtml = '';
                $.each(data.list, function (i, item) {
                    var tempHtml = self.templateList;
                    tempHtml = tempHtml.replace(/{index}/gm, i + 1);
                    tempHtml = tempHtml.replace(/{barCode}/gm, item.barCode);
                    tempHtml = tempHtml.replace(/{userName}/gm, item.userName);
                    tempHtml = tempHtml.replace(/{userGender}/gm, item.userGender == "F" ? "女" : '男');
                    tempHtml = tempHtml.replace(/{userAge}/gm, item.userAge);
                    tempHtml = tempHtml.replace(/{userArea}/gm, item.userArea);
                    tempHtml = tempHtml.replace(/{userAddress}/gm, item.userAddress);
                    tempHtml = tempHtml.replace(/{userWeight}/gm, item.userWeight);
                    tempHtml = tempHtml.replace(/{userStature}/gm, item.userStature);
                    tempHtml = tempHtml.replace(/{projectName}/gm, item.projectName);
                    tempHtml = tempHtml.replace(/{createTime}/gm, item.createTime);
                    tempHtml = tempHtml.replace(/{chained}/gm, item.chained == 1 ? '是' : '否');


                    if (item.status == 1) {
                        tempHtml = tempHtml.replace(/{status}/gm, "等待送检");
                    }
                    if (item.status == 2) {
                        tempHtml = tempHtml.replace(/{status}/gm, "已送检");
                    }
                    if (item.status == 3) {
                        tempHtml = tempHtml.replace(/{status}/gm, "检测中");
                    }
                    if (item.status == 4) {
                        tempHtml = tempHtml.replace(/{status}/gm, "报告已生成");
                    }

                    var btnTempHtml = '';
                    if (item.status == 4) {
                        btnTempHtml += self.menusbtnTemplate.replace("{clickMethod}", "jsHelper.setPower(" + item.barCode + ")").replace("{title}", "查看报告").replace(/{target}/gm, "");
                    }
                    else {
                        btnTempHtml += '<a href="/user/uploadReport/' + item.barCode + '">生成报告</a>';
                    }

                    if (item.chained != 1) {
                        btnTempHtml += '<a href="/report/chained/' + item.barCode + '">上链</a>';
                    }
                    tempHtml = tempHtml.replace(/{btnItems}/, btnTempHtml);

                    appendHtml += tempHtml;
                });
                $("#table-content").html(appendHtml);

                var pageinate = new hotUtil.paging(".pagination", self.params.pageIndex, 20, data.pageCount, data.totalCount, 7);
                pageinate.init(function (p) {
                    goTo(p, function (page) {
                        self.params.pageIndex = page;
                        self.init();
                    });
                });
            }
        });
    },
    getModel: function (dataId) {
        var model = null;
        if (this.localData != null && this.localData.length > 0) {
            $.each(this.localData, function (i, item) {
                if (item.barCode === dataId) {
                    model = item;
                    return false;
                }
            });
        }
        return model;
    },
    edit: function (dataId) {
        //hotUtil.newTab("user/edit?goodsId=" + dataId, "查看报告[" + dataId + "]");
    },
    search: function () {
        this.params.pageIndex = 1;
        this.params.keyword = $("#keyword").val();
        this.params.status = $("#sl_status").val();
        this.params.barCode = $("#barCode").val();
        this.init();
    },
    searchAll: function () {
        $("#keyword").val("");
        $("#sl_status").val(-1);
        this.params.pageIndex = 1;
        this.params.keyword = "";
        this.params.status = -1;
        this.params.barCode = "";
        this.init();
    }
}